package org.etcsoft.examples.jpa.service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.etcsoft.examples.jpa.domain.Address;
import org.etcsoft.examples.jpa.domain.Employee;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author korman21@gmail.com
 *
 */
@Service
public class EmployeeService {
    @PersistenceContext
    private EntityManager em;

    @Transactional
    public Employee addEmployee() {
        Employee emp = new Employee();
        emp.setId(Long.valueOf(2));
        emp.setName("Rob");

        Address addr = new Address();
        addr.setStreet("645");
        addr.setCity("NewYork");

        emp.setAddress(addr);
        this.em.persist(addr);
        this.em.persist(emp);
        this.em.flush();

        return emp;
    }
}
